#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=54;

int n,m;
bool die[N][N];
LL counts[N][N][4];
const map<char,int> mp={
    {'S',0},{'W',1},{'N',2},{'E',3}
};
const int dx[]={1,0,-1,0};
const int dy[]={0,-1,0,1};
struct Node{
  int x,y,way;
  LL cnt;
  Node(){}
  Node(int x,int y,int way,LL cnt){
    this->x=x;this->y=y;this->way=way;
    this->cnt=cnt;
  }
  bool operator<(const Node& node) const{
    return cnt>node.cnt;
  }
};

int main(){
    scanf("%d%d",&n,&m);
    memset(die,0,sizeof(die));
    for (int i = 0; i < n; i++){
        for(int j=0;j<m;j++){
            int u;
            scanf("%d",&u);
            if(u==1){
                die[i][j]=die[i+1][j]=die[i][j+1]=die[i+1][j+1]=true;
            }
        }
    }
    int x0,y0,xe,ye;
    char way;
    scanf("%d%d%d%d %c",&x0,&y0,&xe,&ye,&way);
    memset(counts,0x5f,sizeof(counts));
    priority_queue<Node> que;
    que.push(Node(x0,y0,mp.at(way),0));
    counts[x0][y0][mp.at(way)]=0;
    while(!que.empty()){
        Node node = que.top();
        que.pop();
        int xc=node.x,yc=node.y;
        int wayc=node.way;
        LL cntc=node.cnt;
        //printf("%d %d %d %d\n",xc,yc,wayc,cntc);
        if(counts[xc][yc][wayc]<cntc){
            continue;
        }
        //turn right
        int wayn=(wayc+1)%4;
        if(cntc+1<counts[xc][yc][wayn]){
            counts[xc][yc][wayn] = cntc+1;
            que.push(Node(xc,yc,wayn,cntc+1));
        }
        //turn left
        wayn=(wayc+3)%4;
        if(cntc+1<counts[xc][yc][wayn]){
            counts[xc][yc][wayn] = cntc+1;
            que.push(Node(xc,yc,wayn,cntc+1));
        }
        //前进
        wayn=wayc;
        for(int step=1;step<=3;step++){
            int xn=xc+step*dx[wayc];
            int yn=yc+step*dy[wayc];
            if(xn>=n||xn<=0) break;
            if(yn>=m||yn<=0) break;
            if(die[xn][yn]) break;
            if(cntc+1<counts[xn][yn][wayn]){
                counts[xn][yn][wayn] = cntc+1;
                que.push(Node(xn,yn,wayn,cntc+1));
            }
        }
    }
    //output
    LL ans=LLONG_MAX;
    LL rule;
    memset(&rule,0x5f,sizeof(rule));
    for(int i=0;i<4;i++){
        ans=min(ans,counts[xe][ye][i]);
    }
    if(ans<rule){
        printf("%d",ans);
    }
    else{
        printf("-1");
    }
    return 0;
}

